package com.micro.train.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.train.entity.TrainCarriage;
import com.micro.train.service.TrainCarriageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列车车厢控制器
 * 
 * @author micro
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/carriages")
@Tag(name = "列车车厢管理", description = "列车车厢相关接口")
public class TrainCarriageController {

    @Autowired
    private TrainCarriageService trainCarriageService;

    /**
     * 测试接口 - 获取所有车厢信息
     */
    @GetMapping("/test")
    @Operation(summary = "测试接口", description = "测试获取所有车厢信息")
    public ResponseEntity<String> test() {
        try {
            List<TrainCarriage> carriages = trainCarriageService.list();
            return ResponseEntity.ok("车厢数量: " + carriages.size());
        } catch (Exception e) {
            log.error("测试接口失败", e);
            return ResponseEntity.status(500).body("错误: " + e.getMessage());
        }
    }
    
    /**
     * 获取所有车厢信息
     */
    @GetMapping
    @Operation(summary = "获取所有车厢信息", description = "分页获取所有车厢信息")
    public ResponseEntity<Map<String, Object>> getAllCarriages(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<TrainCarriage> page = new Page<>(current, size);
            Page<TrainCarriage> result = trainCarriageService.page(page);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取车厢信息成功");
            response.put("data", result);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取车厢信息失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "获取车厢信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
    
    /**
     * 获取所有车厢信息（不分页）
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有车厢信息", description = "获取所有车厢信息，不分页")
    public ResponseEntity<List<TrainCarriage>> getAllCarriagesList() {
        try {
            List<TrainCarriage> carriages = trainCarriageService.list();
            return ResponseEntity.ok(carriages);
        } catch (Exception e) {
            log.error("获取车厢信息失败", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 根据ID获取车厢信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取车厢信息", description = "根据车厢ID获取详细信息")
    public ResponseEntity<Map<String, Object>> getCarriageById(
            @Parameter(description = "车厢ID") @PathVariable Long id) {
        try {
            TrainCarriage carriage = trainCarriageService.getById(id);
            if (carriage == null) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 404);
                response.put("message", "车厢不存在");
                return ResponseEntity.status(404).body(response);
            }
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取车厢信息成功");
            response.put("data", carriage);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取车厢信息失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "获取车厢信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 根据列车ID获取车厢信息
     */
    @GetMapping("/train/{trainId}")
    @Operation(summary = "根据列车ID获取车厢信息", description = "获取指定列车的所有车厢")
    public ResponseEntity<List<TrainCarriage>> getCarriagesByTrainId(
            @Parameter(description = "列车ID") @PathVariable Long trainId) {
        try {
            List<TrainCarriage> carriages = trainCarriageService.getCarriagesByTrainId(trainId);
            return ResponseEntity.ok(carriages);
        } catch (Exception e) {
            log.error("获取车厢信息失败", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 创建车厢信息
     */
    @PostMapping
    @Operation(summary = "创建车厢信息", description = "创建新的车厢信息")
    public ResponseEntity<Map<String, Object>> createCarriage(@RequestBody TrainCarriage carriage) {
        try {
            boolean success = trainCarriageService.save(carriage);
            if (success) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "创建车厢信息成功");
                response.put("data", carriage);
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 500);
                response.put("message", "创建车厢信息失败");
                return ResponseEntity.status(500).body(response);
            }
        } catch (Exception e) {
            log.error("创建车厢信息失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "创建车厢信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 更新车厢信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新车厢信息", description = "更新指定车厢的信息")
    public ResponseEntity<Map<String, Object>> updateCarriage(
            @Parameter(description = "车厢ID") @PathVariable Long id,
            @RequestBody TrainCarriage carriage) {
        try {
            carriage.setCarriageId(id);
            boolean success = trainCarriageService.updateById(carriage);
            if (success) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "更新车厢信息成功");
                response.put("data", carriage);
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 404);
                response.put("message", "车厢不存在");
                return ResponseEntity.status(404).body(response);
            }
        } catch (Exception e) {
            log.error("更新车厢信息失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "更新车厢信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 删除车厢信息
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除车厢信息", description = "逻辑删除指定车厢")
    public ResponseEntity<Map<String, Object>> deleteCarriage(
            @Parameter(description = "车厢ID") @PathVariable Long id) {
        try {
            boolean success = trainCarriageService.removeById(id);
            if (success) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "删除车厢信息成功");
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 404);
                response.put("message", "车厢不存在");
                return ResponseEntity.status(404).body(response);
            }
        } catch (Exception e) {
            log.error("删除车厢信息失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "删除车厢信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
}
